package com.xiaozhengkeji.blog.controller;

import com.xiaozhengkeji.blog.entitys.DocEntity;
import com.xiaozhengkeji.blog.mapper.DocMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class DocController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    DocMapper docMapper;

    @RequestMapping(value = "/document/index", method = RequestMethod.GET)
    public ModelAndView document_index(HttpServletRequest request, HashMap<String, Object> map) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("doc_index");
        List<DocEntity> docEntityList = docMapper.getALLDoc();
        logger.info(String.valueOf(docEntityList.size()));
        modelAndView.addObject("Docs", docEntityList);
        return modelAndView;
    }

    @RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
    public ModelAndView document_show(@PathVariable("id") int id, HttpServletRequest request, HashMap<String, Object> map) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("doc_show_index");
        DocEntity docEntity = docMapper.getByIDToDoc(id);
        modelAndView.addObject("Doc", docEntity);
        return modelAndView;
    }
}
